package cn.exlu.wxcall;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import cn.exlu.wxcall.adapter.UserInfo;
import cn.exlu.wxcall.adapter.UserInfoAdapter;
import cn.exlu.wxcall.util.StringUtil;

public class WXCallDel_Activity extends Activity {
	private Context context;
	ListView lv;
	List<UserInfo> list = new ArrayList<UserInfo>();
	private UserInfoAdapter adapter;
	
	@Override
	protected void onStart() {
		super.onStart();
		// 获取已保存数据
		SharedPreferences sharedPref = context.getSharedPreferences(
		        getString(R.string.data_userinfos), Context.MODE_PRIVATE);
		
		Map<String, ?> userinfosMap = sharedPref.getAll();
		
		list.clear();
		if(userinfosMap !=null){
			for(String key :userinfosMap.keySet()){
				String val = (String)userinfosMap.get(key);
				if(StringUtil.isNullOrEmpty(val)){
					continue;
				}
				
				UserInfo userinfo = new UserInfo(val);
				
				if(StringUtil.isNullOrEmpty(userinfo.getUserName())){
					continue;
				}
				if(StringUtil.isNullOrEmpty(userinfo.getUserPhone())){
					continue;
				}
				
				list.add(userinfo);
			}
		}
		adapter = (UserInfoAdapter)lv.getAdapter();
		adapter.notifyDataSetChanged();

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wxcall_del_);
		context = this;
		
		lv =(ListView)this.findViewById(R.id.listViewUserInfo);
		
		lv.setAdapter(new UserInfoAdapter(this, R.layout.wxcall_list_item, list));
		
		lv.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 删除持久化数据
				// 获取已保存数据
				SharedPreferences sharedPref = context.getSharedPreferences(
				        getString(R.string.data_userinfos), Context.MODE_PRIVATE);
//				Map<String, ?> userinfosMap = sharedPref.getAll();
//				if(userinfosMap !=null){
//					
//				}
				Map<String, ?> userinfosMap = sharedPref.getAll();
				UserInfo userInfo = list.get(position);
				SharedPreferences.Editor editor = sharedPref.edit();
				editor.remove(userInfo.getUserPhone());
				editor.commit();
				
				userinfosMap = sharedPref.getAll();

				list.remove(position);
				adapter.notifyDataSetChanged();
			}
			
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.wxcall_del_, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_list) {
			this.finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
